//
//  Controller.m
//  amBXCocoa
//
//  Created by João Pedro Estilita Antunes on 23/11/09.
//  Copyright 2009 João Estilita. All rights reserved.
//  j.estilita@gmail.com
//

#import "Controller.h"

bool connected = false;
//bool enabled = false;
bool illuminator = false;
bool getNewValues=true;
int sleepTime = 1000;
int brightnessByte = 127;
int redMaxBrightness = 127;
int greenMaxBrightness = 127;
int blueMaxBrightness = 127;


@implementation Controller

static Controller *ControllerManager = nil;

+ (Controller*)sharedManager
{
    if (ControllerManager == nil) {
        ControllerManager = [[super allocWithZone:NULL] init];
    }
	
    return ControllerManager;
}


- (IBAction)setFansSpeed:(id)sender
{	
	[usb sendFanSpeed:[left_fan_slider intValue] :[right_fan_slider intValue]];
}

- (IBAction)rumblePadSpeed:(id)sender
{	
	[usb rumblePadSpeed:[rumble_left_slider intValue]: [rumble_right_slider intValue]];
}


- (IBAction)sleepTimeControl:(id)sender
{	
	sleepTime = [sleep_slider intValue];
}

- (IBAction)brightnessByteControl:(id)sender
{	
	brightnessByte = [brightness_slider intValue];
	[self temperatureControl:(id)sender];
}

- (IBAction)temperatureControl:(id)sender
{	
	int temperature;
	temperature = [temperature_comboBox indexOfSelectedItem];
	if (temperature == 0) {
		redMaxBrightness = brightnessByte;
		greenMaxBrightness = brightnessByte;
		blueMaxBrightness = brightnessByte;
	}else if (temperature == 1) {
		redMaxBrightness = 1;
		greenMaxBrightness = brightnessByte;
		blueMaxBrightness = brightnessByte;
		if (greenMaxBrightness > blueMaxBrightness) {
			greenMaxBrightness = blueMaxBrightness;
		}
	}else if (temperature == 2) {
		redMaxBrightness = brightnessByte;
		greenMaxBrightness = brightnessByte;
		blueMaxBrightness = 1;
		if (greenMaxBrightness > redMaxBrightness) {
			greenMaxBrightness = redMaxBrightness;
		}
	}
}

-(void)sendAleatory
{
	int red = 0;
	int green = 0;
	int blue = 0;
	int newRed = 0;
	int newGreen =0;
	int newBlue =0;
	
	srand((unsigned)time(0)); 
	
	for( ; ; )
	{
		if ([illuminator_button selectedSegment] == 1 && [enable_button selectedSegment] == 1 ) 
		{
			if (getNewValues) {
				
				newRed  = arc4random() % redMaxBrightness;
				newGreen  = arc4random() % greenMaxBrightness;
				newBlue  = arc4random() % blueMaxBrightness;
			}			
			
			if (red > newRed)
			{
				red = red-1;
			}else if (red < newRed){
				red = red+1;
			}
			
			if (green > newGreen)
			{
				green = green-1;
			}else if (green < newGreen){
				green = green+1;
			}
			
			if (blue > newBlue)
			{
				blue = blue-1;
			}else if (blue < newBlue){
				blue = blue+1;
			}
			
			if (red == newRed && green == newGreen && blue==newBlue) {
				getNewValues = true;
			}else {
				getNewValues = false;
			}
			
			[usb setRed_global:red];		//values on the slider are from 0:100 so convert to 0:255
			[usb setGreen_global:green];	//values on the slider are from 0:100 so convert to 0:255
			[usb setBlue_global:blue];	//values on the slider are from 0:100 so convert to 0:255
			
			[usb sendValuesToSelectedLight:[speaker_left intValue]:[washer_left intValue]:[washer_center intValue]:[washer_right intValue]:[speaker_right intValue]];
		}
		usleep(sleepTime);
	}
}


- (IBAction)sendPanelColor2selectedlights:(id)sender;
{	
	
	if ([enable_button selectedSegment]==1) {
		//enabled=true;
		
		int red_updated;
		int green_updated;
		int blue_updated;
		NSColor *newColor;
		NSColorWell *colorWell = sender;
		newColor = [colorWell color];
		
		[usb setRed_global:[newColor redComponent]*127];
		[usb setGreen_global:[newColor greenComponent]*127];
		[usb setBlue_global:[newColor blueComponent]*127];
		
		red_updated = [usb UpdateRed]*100/127;			//values on the program are from 0:255 so convert to 0:100
		
		green_updated = [usb UpdateGreen]*100/127;		//values on the program are from 0:255 so convert to 0:100
		
		blue_updated = [usb UpdateBlue]*100/127;		//values on the program are from 0:255 so convert to 0:100
		
		[red_text setIntValue:red_updated];
		[green_text setIntValue:green_updated];
		[blue_text setIntValue:blue_updated];	
		
		[usb sendValuesToSelectedLight:[speaker_left intValue]:[washer_left intValue]:[washer_center intValue]:[washer_right intValue]:[speaker_right intValue]];
		
		[usb sendFanSpeed:[left_fan_slider intValue] :[right_fan_slider intValue]];
		
		[usb rumblePadSpeed:[rumble_left_slider intValue]: [rumble_right_slider intValue]];
		
	}else {
		//enabled=false;
		
		[usb setRed_global:0];		//values on the slider are from 0:100 so convert to 0:255
		
		[usb setGreen_global:0];	//values on the slider are from 0:100 so convert to 0:255
		
		[usb setBlue_global:0];	//values on the slider are from 0:100 so convert to 0:255
		
		[usb sendValuesToSelectedLight:1:1:1:1:1];
		
		[usb sendFanSpeed:0 :0];
		
		[usb rumblePadSpeed:0 :0];
		
	}
}


- (IBAction)sendvalues2selectedlights:(id)sender
{	
	
	if ([enable_button selectedSegment]==1) {
		//enabled=true;
		
		int red_updated;
		int green_updated;
		int blue_updated;
		
		[usb setRed_global:[red_slider intValue]*127/100];		//values on the slider are from 0:100 so convert to 0:255
		
		[usb setGreen_global:[green_slider intValue]*127/100];	//values on the slider are from 0:100 so convert to 0:255
		
		[usb setBlue_global:[blue_slider intValue]*127/100];	//values on the slider are from 0:100 so convert to 0:255
		
		red_updated = [usb UpdateRed]*100/127;			//values on the program are from 0:255 so convert to 0:100
		
		green_updated = [usb UpdateGreen]*100/127;		//values on the program are from 0:255 so convert to 0:100
		
		blue_updated = [usb UpdateBlue]*100/127;		//values on the program are from 0:255 so convert to 0:100
		
		[red_text setIntValue:red_updated];
		[green_text setIntValue:green_updated];
		[blue_text setIntValue:blue_updated];	
		
		[usb sendValuesToSelectedLight:[speaker_left intValue]:[washer_left intValue]:[washer_center intValue]:[washer_right intValue]:[speaker_right intValue]];
		
		[usb sendFanSpeed:[left_fan_slider intValue] :[right_fan_slider intValue]];
		
		[usb rumblePadSpeed:[rumble_left_slider intValue]: [rumble_right_slider intValue]];
		
	}else {
		//enabled=false;
		
		[usb setRed_global:0];		//values on the slider are from 0:100 so convert to 0:255
		
		[usb setGreen_global:0];	//values on the slider are from 0:100 so convert to 0:255
		
		[usb setBlue_global:0];	//values on the slider are from 0:100 so convert to 0:255
		
		[usb sendValuesToSelectedLight:1:1:1:1:1];
		
		[usb sendFanSpeed:0 :0];
		
		[usb rumblePadSpeed:0 :0];
		
	}	
	
}



- (IBAction)opendevice:(id)sender
{	
	
	if (!connected) {
		usb = [[USB_cocoa alloc]init];
		[usb OpenDevice];
		connected = true;
		[status setStringValue:@"Connected!"];
	}
	
	illuminator = true;
	
	thread = [[NSThread alloc] initWithTarget:self
									 selector:@selector(sendAleatory)
									   object:nil];
	[thread start];
	
}


- (IBAction)quit:(id)sender
{	
	//enabled=false;
	
	[usb setRed_global:0];		//values on the slider are from 0:100 so convert to 0:255
	
	[usb setGreen_global:0];	//values on the slider are from 0:100 so convert to 0:255
	
	[usb setBlue_global:0];	//values on the slider are from 0:100 so convert to 0:255
	
	[usb sendValuesToSelectedLight:1:1:1:1:1];
	[usb CloseDevice];
}

@end


